package Service;


import Dao.GoodsCategoryDao;
import Dto.GoodsCategory;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsCategoryService {
    private GoodsCategoryDao goodsCategoryDao = new GoodsCategoryDao();
    public Map<String,Object> selectCategoryByPage(Integer pageNum) throws SQLException {
        List<GoodsCategory> list = goodsCategoryDao.selectCategoryList(pageNum);
        Long totalCount = goodsCategoryDao.selectCategoryCount();
        Long totalPageNum = totalCount==0?1:(totalCount%10==0?totalCount/10:totalCount/10+1);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("totalCount",totalCount);
        map.put("totalPageNum",totalPageNum);
        return map;
    }
    //商品类别添加
    public void addCategory(GoodsCategory goodsCategory) throws SQLException {
        goodsCategoryDao.addCategory(goodsCategory);
    }
    //商品类别预修改
    public GoodsCategory preUpdateCategory(Integer id) throws SQLException {
        GoodsCategory category = goodsCategoryDao.preUpdateCategory(id);
        return category;
    }
    //商品类别修改
    public void updateCategory(GoodsCategory goodsCategory) throws SQLException {
        goodsCategoryDao.updateCategory(goodsCategory);
    }
    //商品类别删除
    public void deleteCategory(Integer id) throws SQLException {
        goodsCategoryDao.deleteCategory(id);
    }
    //模糊查询
    public Map<String,Object> selectAll(String name) throws SQLException {
        Map<String,Object> map = new HashMap<>();
        if(name!=null&&!name.equals("")){
            List<GoodsCategory> nameList = goodsCategoryDao.selectName(name);
            Long totalCount = Long.valueOf(nameList.size());
            Long totalPageNum = totalCount==0?1:(totalCount%10==0?totalCount/10:totalCount/10+1);
            map.put("list",nameList);
            map.put("totalCount",totalCount);
            map.put("totalPageNum",totalPageNum);
            return map;
        }
        List<GoodsCategory> list = goodsCategoryDao.selectCount();
        Long totalCount = goodsCategoryDao.selectCategoryCount();
        Long totalPageNum=(totalCount==0)?1:((totalCount%10==0)?totalCount/10:totalCount/10+1);
        map.put("totalCount", totalCount);
        map.put("totalPageNum", totalPageNum);
        map.put("list", list);
        return map;
    }
}